﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            //string s = ImageToBase64("C:\\Users\\AUTO\\Desktop\\A.p", 300);

            //Console.WriteLine(s);
            string TempPath = "C:\\Users\\AUTO\\Desktop\\test";
             string TempPath2 = "C:\\Users\\AUTO\\Desktop\\test2";


           // DirectoryInfo root = new DirectoryInfo(TempPath);
           // FileInfo[] files = root.GetFiles();
           //string path = files[0].FullName;
           //string sss=  Path.GetFileName(path).Split('.')[0]+"_old."+Path.GetFileName(path).Split('.')[1];

              string src = "E:\\test\\src";
        string dest = "E:\\test\\dest";

        //moveFiles(dest, src); // dest -> src
        MoveFolder(TempPath, TempPath2); // src -> dest
        Console.WriteLine("image copy finished!");
        Console.ReadLine();


            Console.ReadKey();
        }

        /// <summary>
        /// Image 转成 base64
        /// </summary>
        /// <param name="fileFullName"></param>
        public static string ImageToBase64(string fileFullName, double leng)
        {
            try
            {
                if (!File.Exists(fileFullName))
                {
                    Console.WriteLine(string.Format("【图片上传】:文件{0}不存在,上传图片操作失败。。。", fileFullName));
                    return "";
                }

                FileInfo fileInfo = new FileInfo(fileFullName);
                double Length = Convert.ToDouble(fileInfo.Length);
                double Size = Length / 1024;  // 单位转换为kb

                if (Size > leng)
                {
                    Console.WriteLine(string.Format("【图片上传】:文件{0}长度:{1}kb,已超过程标准长度:{2}kb,不允许上传，上传图片操作失败", fileFullName, Size, leng));
                    return "";
                }

                Console.WriteLine("【图片上传】:文件长度为：" + Size + "kb");

                //开始把Image 转成 base64
                Bitmap bmp = new Bitmap(fileFullName);
                MemoryStream ms = new MemoryStream();
                var suffix = fileFullName.Substring(fileFullName.LastIndexOf('.') + 1, fileFullName.Length - fileFullName.LastIndexOf('.') - 1).ToLower();
                var suffixName = suffix == "png" ?
                    ImageFormat.Png :
                    suffix == "jpg" || suffix == "jpeg" ? ImageFormat.Jpeg : suffix == "bmp"
                      ? ImageFormat.Bmp
                      : suffix == "gif"
                        ? ImageFormat.Gif
                        : ImageFormat.Jpeg;

                bmp.Save(ms, suffixName);
                leng = ms.Length;
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();

                return Convert.ToBase64String(arr);
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("【图片上传】:把Image 文件{0}转成 base64字符串文件流失败，错误信息:{1}", fileFullName, ex.Message));
                return "";
            }
        }

        /// <summary>
        /// 转移到成功文件夹
        /// </summary>
        /// <param name="fileInfo">文件信息</param>
        /// <returns></returns>
        public static void FileToSuccessPath(string path, string path2)
        {
           //string path = @"c:\temp\MyTest.txt";
           //string path2 = @"c:\temp2\MyTest.txt";
        try
        {
            if (!File.Exists(path))
            {
                // This statement ensures that the file is created,
                // but the handle is not kept.
                using (FileStream fs = File.Create(path)) {}
            }

            // Ensure that the target does not exist.
            if (File.Exists(path2))	
            File.Delete(path2);

            // Move the file.
            File.Move(path, path2);
            Console.WriteLine("{0} was moved to {1}.", path, path2);

            // See if the original exists now.
            if (File.Exists(path))
            {
                Console.WriteLine("The original file still exists, which is unexpected.");
            }
            else
            {
                Console.WriteLine("The original file no longer exists, which is expected.");
            }			
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }

        /// <summary>
        /// 移动文件夹中的所有文件夹与文件到另一个文件夹
        /// </summary>
        /// <param name="sourcePath">源文件夹</param>
        /// <param name="destPath">目标文件夹</param>
        public static void MoveFolder(string sourcePath, string destPath)
        {
            if (Directory.Exists(sourcePath))
            {
                if (!Directory.Exists(destPath))
                {
                    //目标目录不存在则创建
                    try
                    {
                        Directory.CreateDirectory(destPath);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("创建目标目录失败：" + ex.Message);
                    }
                }
                //获得源文件下所有文件
                List<string> files = new List<string>(Directory.GetFiles(sourcePath));
                files.ForEach(c =>
                {
                    string destFile = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
                    //覆盖模式
                    if (File.Exists(destFile))
                    {
                        File.Delete(destFile);
                    }
                    File.Move(c, destFile);
                });
                //获得源文件下所有目录文件
                List<string> folders = new List<string>(Directory.GetDirectories(sourcePath));

                folders.ForEach(c =>
                {
                    string destDir = Path.Combine(new string[] { destPath, Path.GetFileName(c) });
                    //Directory.Move必须要在同一个根目录下移动才有效，不能在不同卷中移动。
                    //Directory.Move(c, destDir);

                    //采用递归的方法实现
                    MoveFolder(c, destDir);
                });
            }
            else
            {
                throw new DirectoryNotFoundException("源目录不存在！");
            }
        }

        }
    }

